$(document).ready(function() {

	$("#name").keydown(function() {
		var e = event || window.event;
		if (e.keyCode == 13) {
			$("#password").focus();
		}

	});

	$("#password").keydown(function(event) {
		var e = event || window.event;
		if (e.keyCode == 13) {
			$.submit();
		}

	});

	$("#submit").click($.submit);

});

jQuery.submit = function() {
	var _name = $("#name").attr("value");
	var _password = $("#password").attr("value");
	var _cpassword = $("#cpassword").attr("value");
	if (_name == '') {
		$("#returninfo").html("<font color=red>请输入用户名。</font>");
		$("#name").focus();
		return;
	}
	if (_password == '') {
		$("#returninfo").html("<font color=red>密码为空。</font>");
		$("#password").focus();
		return;
	}

	$.ajax({
		type : "POST",
		url : "./login",
		data : {
			psn_account : _name,
			psn_password : _password
		}
	}).done(function(xml) {
		var info = $(xml).find("result").text();
		if (info == 'success') {
			$("#returninfo").html("<font color=green>登录成功</font>");
			$.savecookie("name", _name, 365);
			window.location="./";

		}
		if (info == 'failed') {
			$("#returninfo").html("<font color=red>登录失败</font>");
		}

		if (info == 'invailduser') {
			$("#returninfo").html("<font color=red>无效的用户</font>");
		}

		if (info == 'invaildpassword') {
			$("#returninfo").html("<font color=red>密码错误</font>");
		}

	});

};

jQuery.savecookie = function(key, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = key + "=" + escape(value)
			+ ((expiredays == null) ? "" : "; expires=" + exdate.toGMTString());

};
